<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");

require_once 'vars.php';
require_once 'dbCon.php';
require_once 'deleteFile.php';

//"prefix" gibt an was gelöscht werden soll
if(isset($_GET['prefix'])) {
	if (isset($_GET['id']) && isset($_GET['path'])) {
		$path = $documentRoot.$sitePath.$_GET['path'];
		$allowedExtensions = array("jpg","png","gif","jpeg","pdf","swf","flv","avi","wmv","mov");
			if ($path > '') {
				if (!in_array(end(explode(".", strtolower($path))), $allowedExtensions)) {
					$json['error'] = 'No file, or filename/path is incorrect: '.$path;
					$json['result'] = 0;
				} else {
					$json = array('path' => $path);
			
					if (file_exists($path)) {
						if(rmdirr($path)) {
							$json['result'] = 1;
			
							$prefix = $_GET['prefix'];
							//Queries erstellen
							switch ($prefix) {
								case 'ctg':
									$queries = array('categories' => 'UPDATE`'.$db.'`.`ak_categories`'
									.' SET ctg_thumb="", ctg_thumb_enable='. 0 .' '
									.'WHERE ctg_id = '.$_GET['id']);
			
									break;
								case 'prj':
									$queries = array('projects' => 'UPDATE`'.$db.'`.`ak_projects`'
									.' SET prj_thumb="", prj_thumb_enable='. 0 .' '
									.'WHERE prj_id = '.$_GET['id']);
									break;
								case 'btn':
									$queries = array('buttons' => 'UPDATE`'.$db.'`.`ak_buttons`'
									.' SET btn_thumb="", btn_thumb_enable='. 0 .' '
									.'WHERE btn_id = '.$_GET['id']);
									break;
							}
							foreach($queries as $key => $query) {
								if($result = mysql_query($query)) {
									$json['result_'.$key] = 1;
									$json['result'] = 1;
								} else {
									$json['error_'.$key] = 'delete '.$key.' failed: '.mysql_error();
									$json['error'] = 'delete failed: '.mysql_error();
									$json['result'] = 0;
								}
							}
						} else {
							$json['error'] = 'delete thumb failed';
							$json['result'] = 0;
						}
					} else {
						$json['error'] = 'no thumb found';
						$json['result'] = 0;
					}
				}
			}
	} else {
		$json = array('result' => 0,
						'error' => 'Parameter missing! ' 
						.'No "id" and/or no "path" submitted');
	}
} else {
	$json = array('result' => 0,
					'error' => 'Parameter missing! No type submitted!');
}
// Datenbankverbindung schließen
mysql_close($conn);
$encoded = json_encode($json);
echo $encoded;
unset($encoded);
?>